package com.snowflake.ecatalog.service.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import com.snowflake.ecatalog.contant.Status;
import com.snowflake.ecatalog.entity.Company;
import com.snowflake.ecatalog.entity.Profile;
import com.snowflake.ecatalog.repository.ProfileDao;
import com.snowflake.ecatalog.repository.UserDao;
import com.snowflake.ecatalog.util.LoginUtil;

@Service
@Transactional(readOnly = true)
public class ProfileService {
	@Autowired
	private ProfileDao profileDao;
	@Autowired
	private UserDao userDao;

	public Page<Profile> getProfiles(Map<String, Object> filterParams, int pageNumber, int pageSize) {
		PageRequest pageRequest = buildPageRequest(pageNumber, pageSize);
		Specification<Profile> spec = buildSpecification(filterParams);
		return profileDao.findAll(spec, pageRequest);
	}

	
	public List<Profile> getProfilesOfUser(Long userId){
		return userDao.findOne(userId).getProfiles();
	}
	
	public List<Profile> getEnableProfilesOfUser(Long userId){
		List<Profile> enableProfiles = new ArrayList<Profile>();
		List<Profile> profiles = getProfilesOfUser(userId);
		if(!CollectionUtils.isEmpty(profiles)){
			for(Profile p : profiles){
				if(p.getStatus() == Status.ENABLE){
					enableProfiles.add(p);
				}
			}
		}
		return enableProfiles;
	}

	public Profile getProfile(Long id) {
		return profileDao.findOne(id);
	}

	@Transactional(readOnly = false)
	public void saveProfile(Profile profile) {
		profile.setCreator(LoginUtil.getCurrentUser());
		profile.setCreatedTime(new Date());
		profile.setStatus(Status.ENABLE);
		// 过滤Company空的Id
		filterNullCompanyId(profile);

		profileDao.save(profile);
	}

	@Transactional(readOnly = false)
	public void updateProfile(Profile profile) {
		// 过滤Company空的Id
		filterNullCompanyId(profile);

		profile.setLastModifier(LoginUtil.getCurrentUser());
		profile.setLastModifiedTime(new Date());
		profileDao.save(profile);
	}

	public List<Profile> getEnabledProfile(List<Long> exludeIdList) {
		List<Profile> profileList = null;
		if (CollectionUtils.isNotEmpty(exludeIdList)) {
			profileList = profileDao.findByStatusAndIdNotIn(Status.ENABLE, exludeIdList);
		} else {
			profileList = profileDao.findByStatus(Status.ENABLE);
		}
		return profileList;
	}

	// 过滤Company空的Id
	private void filterNullCompanyId(Profile profile) {
		CollectionUtils.filter(profile.getCompanys(), new org.apache.commons.collections.Predicate() {

			@Override
			public boolean evaluate(Object object) {
				Company company = (Company) object;
				return (null != company.getId());
			}

		});
	}
	
	@Transactional(readOnly = false)
	public void enableProfile(Long id) {
		profileDao.changeStatus(Status.ENABLE, id);
	}

	@Transactional(readOnly = false)
	public void disableProfile(Long id) {
		profileDao.changeStatus(Status.DISABLE, id);
	}

	@Transactional(readOnly = false)
	public void deleteProfile(Long id) {
		profileDao.delete(id);
	}
	
	public boolean existUserOrCompanyInProfile(Long id){
		Profile profile = profileDao.findOne(id);
		return CollectionUtils.isNotEmpty(profile.getCompanys()) || CollectionUtils.isNotEmpty(profile.getUsers());
	}
	
	/**
	 * 创建分页请求.
	 */
	private PageRequest buildPageRequest(int pageNumber, int pagzSize) {
		return new PageRequest(pageNumber - 1, pagzSize);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<Profile> buildSpecification(Map<String, Object> filterParams) {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		Specification<Profile> spec = DynamicSpecifications.bySearchFilter(filters.values(),
				Profile.class);
		return spec;
	}
}
